import useUserStore from "@/store/modules/user";
import XEUtils from "xe-utils";

function authPermission(permission: any) {
    const all_permission = "*:*:*";
    const roleMode = useUserStore().mode;
    if (permission && permission.length > 0) {
        if (roleMode === "multiple") {
            //多角色权限
            const rolePermissions = useUserStore().rolePermissions;
            const role = useUserStore().role;
            const rolePermissionsFilter = XEUtils.get(rolePermissions, role["roleKey"]) as Array<any>;
            return rolePermissionsFilter.some((v: any) => {
                return all_permission === v || v === permission;
            });
        } else {
            const permissions = useUserStore().permissions;
            return permissions.some((v: any) => {
                return all_permission === v || v === permission;
            });
        }
    } else {
        return false;
    }
}

function authPermissionRoute(permission: any) {
    const all_permission = "*:*:*";
    if (permission && permission.length > 0) {
        const permissions = useUserStore().permissions;
        return permissions.some((v: any) => {
            return all_permission === v || v === permission;
        });
    } else {
        return false;
    }
}

function authRole(role: any) {
    const super_admin = "admin";
    const roles = useUserStore().roles;
    if (role && role.length > 0) {
        return roles.some((v: any) => {
            return super_admin === v || v === role;
        });
    } else {
        return false;
    }
}

export default {
    // 验证用户是否具备某权限
    hasPermi(permission: any) {
        return authPermission(permission);
    },
    // 验证用户是否含有指定权限，只需包含其中一个
    hasPermiOr(permissions: any) {
        return permissions.some((item: any) => {
            return authPermission(item);
        });
    },
    // 验证用户是否含有指定权限，必须全部拥有
    hasPermiAnd(permissions: any) {
        return permissions.every((item: any) => {
            return authPermission(item);
        });
    },
    // 验证用户是否具备某角色
    hasRole(role: any) {
        return authRole(role);
    },
    // 验证用户是否含有指定角色，只需包含其中一个
    hasRoleOr(roles: any) {
        return roles.some((item: any) => {
            return authRole(item);
        });
    },
    // 验证用户是否含有指定角色，必须全部拥有
    hasRoleAnd(roles: any) {
        return roles.every((item: any) => {
            return authRole(item);
        });
    },

    // 验证用户是否含有指定权限，只需包含其中一个(路由时使用)
    hasPermiOrRoute(permissions: any) {
        return permissions.some((item: any) => {
            return authPermissionRoute(item);
        });
    },
};
